﻿<?php
//TODO:1- ayah sequence problem: solve it by duplicating ayas 
//TODO:2- remove ayas from the beginning of paragID
//TODO:3- add the files to mediawiki

$file = "1002885.xml";
$xmlread = new XMLReader;
$xmlread->open($file);
$main = "_";
$out = "";

while ($xmlread->read() && $xmlread->name !== $main);

// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($xmlread->name === $main)
{
    $content = $xmlread->readOuterXML();
    $node = new SimpleXMLElement($content);
    $tree = $node["TreeID"];
    $pid  = $node["ParagID"];
    if ($tree != 3 && $tree < 6 || $pid < 392)
    {
      $xmlread->next($main);
      continue;
    }
    $search = array("/<nl\/>/",  //1
                    "/<mm>\/\d<\/mm>/", //2
                    "/<as>\s?=?\s?(.*)=?\s?\<I n=\"(\d+)\" f=\"(\d+).*\" t=\"(\d+)\"\/><\/as>/", //3
                    "/<\/?\_[^\>]*>/", //4
                    "/<J>\=?([^\=]*)\=?([^\=]*)\=?([^\=]*)\<\/J>/", //5
                    "/<\/?HC?>/", //6
                    "/<pa n=\"(\d+)\"\/>/", //7
                    "/<mm>\/\d+\<\/mm>/", //8
                    "/<PA n\=\"(\d+)\"\/>/"); //9
    $replac = array("\n", //1
                    "", //2
                    "[[$2_$3 | $1 ]] ", //3
                    "", //4
                    "== $1$2$3$4 ==", //5
                    "", //6
                    "\n(صفحه $1)\n", //7
                    "", //8
                    " REF_$1 "); //9
    $content = preg_replace($search, $replac, $content);
    $search = "/<P n\=\"(\d+)\">(.*)<\/P>/";
    if (preg_match($search, $content, &$matches) == 1)
      str_replace("REF_" . $matches[0], $matches[1], $out);
    preg_replace($search, "", $content);
    $out .= $content;
    if ($pid == 415)
      break;
    $xmlread->next($main);
}
echo $out;

  function xml() 
  {
    $linkfile = "links.xml";
    $metadataFile = "../quran-data.xml";
    $num_file = "../numbers.txt";
    $dataItems = Array("index", "start", "ayas", "name", "tname", "ename", "type", "order", "rukus");

    $quranData = file_get_contents($metadataFile);
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $quranData, $values, $index);
    xml_parser_free($parser);

    for ($i=1; $i<=114; $i++) 
    {
      $j = $index['SURA'][$i-1];
      foreach ($dataItems as $item)
        $suraData[$i][$item] = $values[$j]['attributes'][strtoupper($item)]; 
    }
    
    $linkData = file_get_contents($linkfile);
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $linkData, $values, $index);
    xml_parser_free($parser);

    print_r($values[1]);
    //print_r($index);
  }

  function get_arabic_number($str)
  {
		global $num_file;
    $numText = file($num_file);
    $arabic_numbers = explode(" ", $numText[0]);
    $ar_num = "";
    for ($i=0; $i<strlen($str); $i++)
    {
			if ($str[$i] >='0' && $str[$i] <='9')
      	$ar_num .= $arabic_numbers[((int) $str[$i]) + 1];
			else
				break;
    }
    return $ar_num;
  }

  function english($str)
  {
    $numText = "#0123456789";
    $english_numbers = explode(" ", $numText[0]);
    $ar_num = "";
    for ($i=0; $i<strlen($str); $i++)
    {
			if ($str[$i] >='0' && $str[$i] <='9')
      	$ar_num .= $english_numbers[((int) $str[$i]) + 1];
			else
				break;
    }
    return $ar_num;
  }
?>
